{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "90d0b258",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c079e93c",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array ([1,2,3,4,5])\n",
    "b = np.array([range(1,6)])\n",
    "c = np.arange(1,6)\n",
    "d = np.arange(1,6,dtype=float)\n",
    "#np.array的用法：arange([start,]stop[,step],dtype=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "369f5857",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a78cf32d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4, 5]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2f660a2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fc9dc49c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "40b48d3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "14c86ef3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5.])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "eabee9b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d5c52a42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4c26d675",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5], dtype=int8)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.astype(\"int8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4f8915ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.42611885 0.72810002 0.77787507 0.65791679 0.56016918 0.33070836\n",
      " 0.05202683 0.64646961 0.7332984  0.80347202]\n"
     ]
    }
   ],
   "source": [
    "e = np.array([random.random() for i in range(10)])\n",
    "print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3726cc71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.43, 0.73, 0.78, 0.66, 0.56, 0.33, 0.05, 0.65, 0.73, 0.8 ])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(e,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bd3914c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = np.array([[0,1,2],[3,4,5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1f15779a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3e8e103d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "637afdf3",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = np.arange(12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "55bd13d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.reshape((3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "id": "87f73038",
   "metadata": {},
   "outputs": [],
   "source": [
    "h = np.arange(24).reshape(2,3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7bc1e973",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]],\n",
       "\n",
       "       [[12, 13, 14, 15],\n",
       "        [16, 17, 18, 19],\n",
       "        [20, 21, 22, 23]]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "id": "0b856a3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19, 20, 21, 22, 23])"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#0是行数，1是列数，2是第三维度\n",
    "h.reshape(h.shape[0]*h.shape[1]*h.shape[2],)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "00b63548",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19, 20, 21, 22, 23])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#把数据转为1维数组\n",
    "h.flatten()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7048b8a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 2,  3,  4,  5],\n",
       "        [ 6,  7,  8,  9],\n",
       "        [10, 11, 12, 13]],\n",
       "\n",
       "       [[14, 15, 16, 17],\n",
       "        [18, 19, 20, 21],\n",
       "        [22, 23, 24, 25]]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h+2    #所有数据+2  广播机制   +-*/都一样  两个数组运算 则对应位置计算   维度不同的时候 也可以计算 前提是有一维是长度是相等的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4a758a2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#切片和索引\n",
    "i = g.reshape((3,4))\n",
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3156d8ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5, 6, 7])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i[1]  #取某一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8e8b305e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 5, 9])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i[:,1]  #取某一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "da780f9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "289f1aa4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  3],\n",
       "       [ 6,  7],\n",
       "       [10, 11]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i[:,2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "4077f0f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2],\n",
       "       [ 4,  6],\n",
       "       [ 8, 10]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i[:,0:4:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "2652be30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2],\n",
       "       [ 4,  6],\n",
       "       [ 8, 10]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i[:,[0,2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f22d088c",
   "metadata": {},
   "outputs": [],
   "source": [
    "i[:,0:2] = 0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "bb6c1ddd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  2,  3],\n",
       "       [ 0,  0,  6,  7],\n",
       "       [ 0,  0, 10, 11]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3cea9c25",
   "metadata": {},
   "outputs": [],
   "source": [
    "i[i<5] = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3d99a965",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 10, 10, 10],\n",
       "       [10, 10,  6,  7],\n",
       "       [10, 10, 10, 11]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4f11386c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0],\n",
       "       [0, 0, 2, 2],\n",
       "       [0, 0, 0, 2]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(i==10,0,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "5c17e831",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 10, 10, 10],\n",
       "       [10, 10, 10, 10],\n",
       "       [10, 10, 10, 11]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i.clip(10,18)  #小于10的替换成10，大于18的替换成18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "2520a979",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 2], dtype=int64)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 竖直拼接np.vstack\n",
    "#水平拼接np.hstack\n",
    "\n",
    "#获取最大值最小值的位置\n",
    "np.argmax(i,axis=0)    #axis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "37da0156",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 0], dtype=int64)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmin(i,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "7371cde3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建一个全为0的数组\n",
    "np.zeros((3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "11307041",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创造一个全为1的数组\n",
    "np.ones((3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "a9579d4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创造一个对角线为1的正方形数组\n",
    "np.eye(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "3f70ef90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19, 10, 17, 19, 18],\n",
       "       [16, 10, 13, 18, 16],\n",
       "       [18, 16, 18, 12, 10],\n",
       "       [15, 14, 19, 19, 15]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(10,20,(4,5))  #low,high,shape为三个参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "158d96e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 9,  4, 15,  0],\n",
       "       [17, 16, 17,  8],\n",
       "       [ 9,  0, 10,  8]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(10)\n",
    "y = np.random.randint(0,20,(3,4))\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "bb4eb2f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  C_CONTIGUOUS : True\n",
      "  F_CONTIGUOUS : True\n",
      "  OWNDATA : True\n",
      "  WRITEABLE : True\n",
      "  ALIGNED : True\n",
      "  WRITEBACKIFCOPY : False\n",
      "  UPDATEIFCOPY : False\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "x = np.array([1,2,3,4,5])  \n",
    "print (x.flags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "1eb3d7f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 808994312,  792345136, 1174960694])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.empty([3,],dtype = int,order = \"C\")\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "a7dd9287",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.zeros([3,2],dtype = [(\"age\",'float'),(\"time\",'i4')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "81db4084",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[(0, 0.), (0, 0.)],\n",
       "       [(0, 0.), (0, 0.)]], dtype=[('x', '<i4'), ('y', '<f4')])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "c534a059",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1 1 1]\n",
      " [1 1 1 1 1]\n",
      " [1 1 1 1 1]]\n"
     ]
    }
   ],
   "source": [
    " \n",
    "# 含有 5 个 1 的数组，默认类型为 float  \n",
    "import numpy as np \n",
    "x = np.ones([3,5],dtype=int)  \n",
    "print (x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "d40e8d36",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1]\n",
      " [1 1]]\n"
     ]
    }
   ],
   "source": [
    " \n",
    "import numpy as np \n",
    "x = np.ones([2,2], dtype =  int)  \n",
    "print (x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "73e3bd3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "b = [(1,2,3),(4,5)]\n",
    "print(isinstance(b, np.ndarray)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "d2d8c580",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([(1, 2, 3), (4, 5)], dtype=object)"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.asarray(a)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "02402bbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2,)"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "04a5549c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('O')"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "00e85b63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 2, 3) (4, 5)]\n"
     ]
    }
   ],
   "source": [
    "print (x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "ac03255b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(isinstance(x, np.ndarray)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "e5e42ebe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "y = np.asarray(b)\n",
    "print(isinstance(y, np.ndarray)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "9c6f1bb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "s =  b'Hello World' \n",
    "a = np.frombuffer(s, dtype =  'S1')  \n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "38ecfe0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([b'H', b'e', b'l', b'l', b'o', b' ', b'W', b'o', b'r', b'l', b'd'],\n",
       "      dtype='|S1')"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "4e88785b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10.  12.5 15.  17.5 20. ]\n"
     ]
    }
   ],
   "source": [
    " \n",
    "import numpy as np\n",
    "x = np.linspace(10,20,5)  \n",
    "print (x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "id": "5e8d1964",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10. 12. 14. 16. 18.]\n"
     ]
    }
   ],
   "source": [
    "x = np.linspace(10,20,5,endpoint = False)  \n",
    "print (x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "4d9f4f4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 10.          12.91549665  16.68100537  21.5443469   27.82559402\n",
      "  35.93813664  46.41588834  59.94842503  77.42636827 100.        ]\n"
     ]
    }
   ],
   "source": [
    " \n",
    "import numpy as np\n",
    "# 默认底数是 10\n",
    "a = np.logspace(1.0,  2.0, num =  10)  \n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "ab33b147",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   2.,    4.,    8.,   16.,   32.,   64.,  128.,  256.,  512.,\n",
       "       1024.])"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将对数空间的底数设置为 2  \n",
    "import numpy as np\n",
    "a = np.logspace(1,10,num =  10,  base  =  2)  \n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "fcf706bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2],\n",
       "       [ 9, 11]])"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " \n",
    "import numpy as np \n",
    "x = np.array([[  0,  1,  2],[  3,  4,  5],[  6,  7,  8],[  9,  10,  11]])  \n",
    "rows = np.array([[0,0],[3,3]]) \n",
    "cols = np.array([[0,2],[0,2]]) \n",
    "y = x[rows,cols]  \n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "8de260fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [3, 3]])"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "06ab5683",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2],\n",
       "       [0, 2]])"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "336c93e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5.])"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " \n",
    "import numpy as np \n",
    "a = np.array([np.nan,  1,2,np.nan,3,4,5])  \n",
    "a[~np.isnan(a)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "45ccfe50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2. +6.j, 3.5+5.j])"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " \n",
    "import numpy as np \n",
    "a = np.array([1,  2+6j,  5,  3.5+5j])  \n",
    "a[np.iscomplex(a)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "011028f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "5\n",
      "10\n",
      "15\n",
      "20\n",
      "25\n",
      "30\n",
      "35\n",
      "40\n",
      "45\n",
      "50\n",
      "55\n"
     ]
    }
   ],
   "source": [
    " \n",
    "import numpy as np\n",
    "a = np.arange(0,60,5) \n",
    "a = a.reshape(3,4)    \n",
    "for x in np.nditer(a):  \n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "2ecd3651",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(55)"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "49e73d63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "2830cd9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:1\n",
      "5:2\n",
      "10:3\n",
      "15:4\n",
      "20:1\n",
      "25:2\n",
      "30:3\n",
      "35:4\n",
      "40:1\n",
      "45:2\n",
      "50:3\n",
      "55:4\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    " \n",
    "import numpy as np \n",
    "a = np.arange(0,60,5) \n",
    "a = a.reshape(3,4)  \n",
    "b = np.array([1,  2,  3,  4], dtype =  int)  \n",
    "for x,y in np.nditer([a,b]):  \n",
    "    print  (\"%d:%d\"  %  (x,y))\n",
    "print(type(y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "887b28ee",
   "metadata": {},
   "outputs": [],
   "source": [
    " \n",
    "import numpy as np \n",
    "x = np.array([[1], [2], [3]]) \n",
    "y = np.array([4, 5, 6])  \n",
    "   \n",
    "# 对 y 广播 x\n",
    "b = np.broadcast(x,y)  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "id": "e98a9dc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "id": "d6f285ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "r, c = b.iters  # 获取迭代器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "id": "db64d650",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在插入之前输入数组会被展开。\n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "\n",
      "\n",
      "传递了 Axis 参数。 会广播值数组来配输入数组。\n",
      "沿轴 0 广播：\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n",
      "沿轴 1 广播：\n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n"
     ]
    }
   ],
   "source": [
    " \n",
    "import numpy as np \n",
    "a = np.array([[1,2],[3,4],[5,6]]) \n",
    " \n",
    "print ('第一个数组：') \n",
    "print (a )\n",
    "print ('\\n' ) \n",
    " \n",
    "print ('未传递 Axis 参数。 在插入之前输入数组会被展开。') \n",
    "print (np.insert(a,3,[11,12]) )\n",
    "print( '\\n' ) \n",
    "print ('传递了 Axis 参数。 会广播值数组来配输入数组。' )\n",
    " \n",
    "print ('沿轴 0 广播：') \n",
    "print (np.insert(a,1,[11],axis = 0) )\n",
    "print( '\\n' ) \n",
    " \n",
    "print ('沿轴 1 广播：' )\n",
    "print (np.insert(a,1,11,axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "id": "8c6e4379",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  0.25   1.33   1.     0.   100.  ]\n",
      "[4.        0.7518797 1.              inf 0.01     ]\n",
      "[100]\n",
      "[0]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ANACONDA\\envs\\cv\\lib\\site-packages\\ipykernel_launcher.py:6: RuntimeWarning: divide by zero encountered in reciprocal\n",
      "  \n"
     ]
    }
   ],
   "source": [
    " #返回倒数\n",
    "import numpy as np \n",
    "a = np.array([0.25,  1.33,  1,  0,  100])  \n",
    " \n",
    "print (a) \n",
    " \n",
    "print (np.reciprocal(a)  )\n",
    "\n",
    "b = np.array([100], dtype =  int)  \n",
    "  \n",
    "print (b) \n",
    "\n",
    "print (np.reciprocal(b)  )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "id": "84bcf80b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 7 5]\n",
      " [8 4 3]]\n",
      "5\n",
      "[4 5]\n",
      "[5 3 2]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import numpy as np \n",
    "a = np.array([[3,7,5],[8,4,3]])  \n",
    "print (a )\n",
    "\n",
    "print (np.ptp(a)  )\n",
    "  \n",
    "print (np.ptp(a, axis =  1)  )\n",
    "  \n",
    "print (np.ptp(a, axis =  0)  )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b0ca02f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
